package org.csu.hotelsys.service;


import org.csu.hotelsys.domain.Check_in;
import org.csu.hotelsys.domain.Customer;
import org.csu.hotelsys.domain.Room_Reserve;
import org.csu.hotelsys.domain.Room;
import org.csu.hotelsys.persistence.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService {
    
    @Autowired
    CustomerMapper customerMapper;





    //根据用户电话获取预定信息,未取消，未入住
    public List<Room_Reserve> getReserveRoomByPhone(String phone){
        String cancal="0"; //未取消
        String check_in="0";//未入住
        return customerMapper.getReserveRoomByPhone(phone,cancal,check_in);
    }


    //获取尚未取消且尚未入住的预定信息表
    public  List<Room_Reserve> getCanReserveRoomList() {
        List<Room_Reserve> tempList=customerMapper.getReserveRoomList();
        for(int i=0;i<tempList.size();i++){
            if(tempList.get(i).getCancle_bool().equals("1")||tempList.get(i).getCheck_in_bool().equals("0")){
                tempList.remove(i);
            }
        }
        return tempList;
    }



    //获取所有预定信息表
    public  List<Room_Reserve> getAllReserveRoomList(String status) {
        return customerMapper.getReserveRoomList();
    }

    //根据预定编号获取预定信息
    public Room_Reserve getReserveRoomByID(String id){
        return customerMapper.getReserveRoomByID(id);
    }



    //获取可预订可入住客房列表
    public List<Room> getavailableRoomList(String status){

        return  customerMapper.getavailableRoomList(status);
    }

    //获取特定Customer信息
    public Customer getCustomerById(String credentials_num){
        return  customerMapper.getCustomerById(credentials_num);
    }
    //获取所有Customer信息
    public List<Customer> getAllCustomer( ){
        return  customerMapper.getAllCustomer();
    }


    //查询所有入住登记列表
    public List<Check_in> getCheck_inList(){

        return customerMapper.getCheck_inList();

    }

    //查询所有未退房入住登记列表
    public List<Check_in> getNoExitCheck_inList(){
        String status="1";
        return customerMapper.getNoExitCheck_inList(status);

    }

    //根据用户身份证获取入住表信息
    public List<Check_in> getCheck_inByCredit(String credentials_num){
        return customerMapper.getCheck_inByCredit(credentials_num);
    }

    //根据check_in_id获取入住表信息
    public Check_in getCheck_inById(String check_in_id){
        return customerMapper.getCheck_inById(check_in_id);
    }

    //客房预定
    public void reserveRoom(Room_Reserve room_reserve){
        String id=room_reserve.getReserve_time();
        id=id+(customerMapper.getNumOfReserve(id)+1);
        room_reserve.setReserver_id(id);
        customerMapper.reserveRoom(room_reserve);


        //更改房间状态
        String status="1";
        Room room=customerMapper.getroom(room_reserve.getReserve_room_id());
        room.setRoom_status(status);
        customerMapper.updateRoom_status(room);
    }


    //修改预定信息的同时修改客房信息
   public void updateReserveRoom(Room_Reserve room_reserve){

       Room beforeroom=customerMapper.getroom(customerMapper.getReserveRoomByID(room_reserve.getReserver_id()).getReserve_room_id());
       customerMapper.updateReserveRoom(room_reserve);
        if(beforeroom.getRoom_id().equals(room_reserve.getReserve_room_id())){
           System.out.println("修改预定信息时未修改房间");
        }
       else{
           //1代表处于预定或入住状态   0代表可预订或入住
           beforeroom.setRoom_status("0");
           customerMapper.updateRoom_status(beforeroom);
            Room afterroom=customerMapper.getroom(room_reserve.getReserve_room_id());
           afterroom.setRoom_status("1");
           customerMapper.updateRoom_status(afterroom);

        }

    }







    //取消预定信息
    public void cancelReserveRoom(Room_Reserve room_reserve){
        customerMapper.cancelReserveRoom(room_reserve);

        //更改房间状态
        String status="0";
        Room room=customerMapper.getroom(room_reserve.getReserve_room_id());
        room.setRoom_status(status);
        customerMapper.updateRoom_status(room);
    }




    //预定转入住,直接新增入住单并返回
    public Check_in reserveInRoom(String reserver_id){

        Room_Reserve room_reserve=customerMapper.getReserveRoomByID(reserver_id);
        customerMapper.reserveRoom(room_reserve);
        //更新房间状态，暂时默认预定和入住对房间来说是一个状态
//        room temproom=customerMapper.getroom(room_reserve.getReserve_room_id());
//        temproom.setRoom_status("0");
//        customerMapper.updateRoom_status(temproom);


        //获取预定信息，可转到Controller
        Check_in check_in=new Check_in();
        check_in.setCheck_id(room_reserve.getCheck_in_time()+(customerMapper.getNumOfIn(room_reserve.getCheck_in_time())+1));
        check_in.setCheck_in_name(room_reserve.getCustomer_name());
        check_in.setCheck_in_time(room_reserve.getCheck_in_time());
        check_in.setCredentials_type(room_reserve.getCredentials_type());
        check_in.setCredentials_num(room_reserve.getCredentials_num());
        check_in.setRoom_id(room_reserve.getReserve_room_id());
        customerMapper.inRoom(check_in);
        return check_in;

    }



    //登记入住
    public void inRoom(Check_in check_in){
        check_in.setCheck_id(check_in.getCheck_in_time()+(customerMapper.getNumOfIn(check_in.getCheck_in_time())+1));
        customerMapper.inRoom(check_in);

        //更改房间状态
        String status="1";
        Room room=customerMapper.getroom(check_in.getRoom_id());
        room.setRoom_status(status);
        customerMapper.updateRoom_status(room);
    }



    //换房
    public void updateCheck_inRoom(Check_in check_in){

        customerMapper.updateCheck_inRoom(check_in);
    }

    //退房
   public void exitRoom(Check_in check_in){

        //Check_in表中CONTINUE_BOOL当做是否退房  0代表退房 1代表未退房
       check_in.setContinue_bool("0");
       customerMapper.updateCheck_inRoom(check_in);

       //更改房间状态
       String status="0";
       Room room=customerMapper.getroom(check_in.getRoom_id());
       room.setRoom_status(status);
       customerMapper.updateRoom_status(room);
    }

    //添加Customer
    public void addCustoemr(Customer customer){
        customerMapper.addCustomer(customer);
    }
    //更改Customer
    public void changeCustoemr(Customer customer){
        customerMapper.changeCustomer(customer);
    }


}
